//
//  WBEmoticonKeyBoardView.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/20.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBEmoticonKeyBoardView.h"
#import "WBEmoticonBottomView.h"
#import <Masonry.h>
#import "WBEmoticonPageView.h"

@interface WBEmoticonKeyBoardView ()<UICollectionViewDelegate>

//底部视图
@property (nonatomic , strong) WBEmoticonBottomView *bottomView;

//分页视图
@property (nonatomic , strong) WBEmoticonPageView *pageView;

@end


@implementation WBEmoticonKeyBoardView

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        
        [self setupUI];
        
    }
    
    return self;
    
}

- (WBEmoticonBottomView *)bottomView
{
    if(!_bottomView)
    {
        
        _bottomView = [[WBEmoticonBottomView alloc] initWithFrame:CGRectZero];
        
    }
    
    return _bottomView;
    
}

- (WBEmoticonPageView *)pageView
{
    if(!_pageView)
    {
        
        _pageView = [[WBEmoticonPageView alloc] init];
        
        _pageView.bounces = NO;
        
        _pageView.showsVerticalScrollIndicator = NO;
        _pageView.showsHorizontalScrollIndicator = NO;
        
        _pageView.pagingEnabled = YES;
        
        //设置代理
        _pageView.delegate = self;
    }
    
    return _pageView;
    
}

#pragma mark - 设置UI
- (void)setupUI
{
    
    self.backgroundColor = [UIColor whiteColor];
    
    //添加控件
    [self addSubview:self.bottomView];
    [self addSubview:self.pageView];
    
    //设置约束
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.trailing.bottom.mas_equalTo(self);
        make.height.mas_equalTo(37);
    }];
    
    [self.pageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.trailing.top.mas_equalTo(self);
        make.bottom.mas_equalTo(self.bottomView.mas_top).mas_offset(0);
        
    }];
    
    __weak typeof(self) weakSelf = self;
    self.bottomView.block = ^(WBEmoticonBottomViewType type){
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:(int)type];
        
        [weakSelf.pageView selectSectionWithIndexPath:indexPath];
        
    };
}

#pragma mark - pageView的代理方法实现 实现上下联动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //判断哪个cell占中心点 计算中心点相对于pageview的坐标
    CGFloat centerX = self.pageView.contentOffset.x + kScreenW * 0.5;
    
    //判断当前界面显示几个cell
    NSArray<UICollectionViewCell *> *cells = [[self.pageView visibleCells] sortedArrayUsingComparator:^NSComparisonResult(UICollectionViewCell *obj1, UICollectionViewCell *obj2) {
        
        return obj1.frame.origin.x < obj2.frame.origin.x;
        
    }];
    
    //说明当前界面只显示一个cell 不用继续做下面的事了
    if(cells.count == 1)
    {
        return ;
    }
    
    //取出第一个cell
    UICollectionViewCell *firstCell = cells.firstObject;
    //取出第二个cell
    UICollectionViewCell *lastCell = cells.lastObject;
    NSIndexPath *indexPath;
    //判断中心点在哪个cell上面
    if(CGRectContainsPoint(firstCell.frame, CGPointMake(centerX, 0)))
    {
        indexPath = [self.pageView indexPathForCell:firstCell];
    }else
    {
        indexPath = [self.pageView indexPathForCell:lastCell];
    }
    
    
    [self.bottomView setSelectedBtnWithTag:(int)indexPath.section + typeNumber];
    
}


@end
